﻿using System;
using System.Web;
using System.Web.SessionState;

using Tools.Msn.China;
using Com.Ompchina.Lib.Text;
using Model.AuthModel;
using Util.Configuration;
namespace Util.Helper
{
    /// <summary>
    /// 核心功能类，辅助操作类，管理员辅助操作
    /// </summary>
    public class StaffHelper : IRequiresSessionState
    {
        /// <summary>
        /// 设置登录用户记录
        /// </summary>
        /// <param name="info"></param>
        public void SetLoginStaff(StaffInfo info)
        {
            this.SetLoginStaff(HttpContext.Current, info);
        }
        /// <summary>
        /// 设置登录用户记录
        /// </summary>
        /// <param name="context"></param>
        /// <param name="info"></param>
        public void SetLoginStaff(HttpContext context, StaffInfo info) 
        {
            if (WebConfiguration.SorC.Equals("s"))
            {                
                //HttpContext.Current.Session.Add("staffid",Security.StrEncrypt(info.StaffID.ToString(), WebConfiguration.Key));
                //HttpContext.Current.Session.Add("staffname", HttpContext.Current.Server.UrlEncode(info.Username));
                SessionManager.SetSession("staffid", Security.StrEncrypt(info.StaffID.ToString(), WebConfiguration.Key), 1000 * 30);
                SessionManager.SetSession("staffname", HttpContext.Current.Server.UrlEncode(info.Username), 1000 * 30);
            }
            else
            {
                CookieManager.SetCookie("staffid", Security.StrEncrypt(info.StaffID.ToString(), WebConfiguration.Key));
                CookieManager.SetCookie("staffname", HttpContext.Current.Server.UrlEncode(info.Username));
            }
        }

        /// <summary>
        /// 获取登录用户对象
        /// </summary>
        /// <returns></returns>
        public StaffInfo GetLoginStaff()
        {
            HttpSessionState Session = HttpContext.Current.Session;
            StaffInfo info = new StaffInfo();
            string saveID = WebConfiguration.SorC.Equals("s") ?
                (Session["staffid"] != null ? Session["staffid"].ToString() : "")
                : CookieManager.GetCookieValue("staffid");
            string saveName = WebConfiguration.SorC.Equals("s") ?
                (Session["staffname"] != null ? Session["staffname"].ToString() : "")
                : CookieManager.GetCookieValue("staffname");
            string strStaffID = string.IsNullOrEmpty(saveID) ? "0" : Security.StrDecrypt(saveID
                , WebConfiguration.Key);                
            string strStaffName = HttpContext.Current.Server.UrlDecode(saveName);
            int staffID = 0;
            int.TryParse(strStaffID, out staffID);
            info.StaffID = staffID;
            info.Username = strStaffName;
            return info;
        }

        /// <summary>
        /// 清除登录用户信息
        /// </summary>
        public void ClearLoginStaff()
        {
            if (WebConfiguration.SorC.Equals("s"))
            {
                SessionManager.RemoveSession("staffid");
                SessionManager.RemoveSession("staffname");
            }
            else
            {
                CookieManager.ClearCookie("staffid", "");
                CookieManager.ClearCookie("staffname", "");
            }
        }
    }
}
